package com.xgq.drink.controller;

import com.xgq.drink.annotation.RequiresRole;
import com.xgq.drink.entity.DiscountInfo;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IDiscountInfoService;
import com.xgq.drink.utils.JwtTokenUtil;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 优惠信息表 前端控制器
 * </p>
 *
 * @author xgq
 * @since 2025-03-04
 */
@RestController
@RequestMapping("/api/discountInfo")
@Tag(name = "优惠券模块", description = "优惠券相关接口")
public class DiscountInfoController {

    @Autowired
    private IDiscountInfoService discountInfoService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @PostMapping("/platform/create")
    @RequiresRole("ADMIN")
    @Operation(summary = "创建平台优惠券", description = "创建平台优惠券或会员优惠券，需要ADMIN角色权限")
    public R createPlatformDiscount(@RequestBody DiscountInfo discountInfo, HttpServletRequest  request) {
        // 验证优惠券类型
        if (!discountInfo.getDiscountType().equals("会员") && !discountInfo.getDiscountType().equals("官方")) {
            return RUtils.fail("无权创建此类型优惠券");
        }
        return discountInfoService.createDiscountInfo(discountInfo);
    }

    @PostMapping("/brand/create")
    @RequiresRole(value = {"STORE_OWNER", "STORE_STAFF"}, brandRequired = true)
    @Operation(summary = "创建店铺优惠券", description = "创建店铺优惠券或商品优惠券，需要STORE_OWNER或STORE_STAFF角色权限")
    public R createBrandDiscount(@RequestBody DiscountInfo discountInfo, HttpServletRequest request) {
        if (!discountInfo.getDiscountType().equals("SHOP") && !discountInfo.getDiscountType().equals("PRODUCT")) {
            return RUtils.fail("无权创建此类型优惠券");
        }
        String token = request.getHeader("Authorization");
        Long brandId = jwtTokenUtil.getStoreIdFromToken(token);
        discountInfo.setShopId(brandId);
        discountInfo.setVersion(0);
        return discountInfoService.createDiscountInfo(discountInfo);
    }

    @PutMapping("/platform/{discountId}/deactivate")
    @Operation(summary = "下架平台优惠券", description = "下架平台优惠券或会员优惠券，需要ADMIN角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R deactivatePlatformDiscount(@PathVariable Long discountId) {
        return discountInfoService.deactivateDiscountInfo(discountId);
    }
    @PutMapping("/platform/{discountId}/deactivate2")
    @Operation(summary = "下架平台优惠券", description = "下架平台优惠券或会员优惠券，需要ADMIN角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R deactivatePlatformDiscount2(@PathVariable Long discountId) {
        return discountInfoService.deactivateDiscountInfo2(discountId);
    }

    @PutMapping("/brand/{discountId}/deactivate")
    @RequiresRole(value = {"STORE_OWNER", "STORE_STAFF"}, brandRequired = true)
    @Operation(summary = "下架店铺优惠券", description = "下架店铺优惠券或商品优惠券，需要STORE_OWNER或STORE_STAFF角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R deactivateBrandDiscount(@PathVariable Long discountId) {
        return discountInfoService.deactivateDiscountInfo(discountId);
    }
    @PutMapping("/brand/{discountId}/deactivate2")
    @RequiresRole(value = {"STORE_OWNER", "STORE_STAFF"}, brandRequired = true)
    @Operation(summary = "下架店铺优惠券", description = "下架店铺优惠券或商品优惠券，需要STORE_OWNER或STORE_STAFF角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R deactivateBrandDiscount2(@PathVariable Long discountId) {
        return discountInfoService.deactivateDiscountInfo2(discountId);
    }


    @PutMapping("/{discountId}/replenish")
    @RequiresRole(value = {"STORE_OWNER", "STORE_STAFF"}, brandRequired = true)
    @Operation(summary = "补充优惠券", description = "补充优惠券数量，需要STORE_OWNER或STORE_STAFF角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R replenishDiscount(
            @PathVariable Long discountId,
            @RequestParam Integer discountNum) {
        return discountInfoService.replenishDiscountInfo(discountId, discountNum);
    }

    @GetMapping("/list")
    @Operation(summary = "查询优惠券", description = "分页查询优惠券，可按类型、店铺ID、商品ID筛选")
    public R getDiscounts(
            @RequestParam(required = false) String discountType,
            @RequestParam(required = false) Long shopId,
            @RequestParam(required = false) Long productId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        System.out.println("查询优惠券");
        return discountInfoService.getDiscountInfos(discountType, shopId, productId, page, size);
    }

    @GetMapping("/{discountId}")
    @Operation(summary = "获取优惠券详情", description = "获取优惠券详细信息")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R getDiscountInfo(@PathVariable Long discountId) {
        return discountInfoService.getDiscountInfo(discountId);
    }

    @PostMapping("/{discountId}/claim")
    @Operation(summary = "领取优惠券", description = "用户领取优惠券")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R claimDiscount(@PathVariable Long discountId) {
        return discountInfoService.claimDiscountInfo(discountId);
    }

    @PutMapping("/edit")
    @RequiresRole(value = {"STORE_OWNER", "STORE_STAFF"}, brandRequired = true)
    @Operation(summary = "更新优惠券", description = "更新优惠券信息，需要STORE_OWNER或STORE_STAFF角色权限")
    @Parameter(name = "discountId", description = "优惠券ID", required = true)
    public R updateDiscount(
            @RequestBody DiscountInfo discountInfo) {
        return discountInfoService.updateDiscountInfo(discountInfo);
    }
}
